// pages/webview/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: [
      'http://101.132.97.213:9090/image/1233212212212/1575952652583827.jpg',
      'http://101.132.97.213:9090/image/1233212212212/1575952653630142.jpg'
    ],
    product: {},
    src1: 'https://mp.fot-tech.com/image/1/test1.mp4',
    src2: 'https://mp.fot-tech.com/image/1/123.mp4',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    province: app.globalData.province,
    city: app.globalData.city,
    county: app.globalData.county,
    carNumber: 0,
    productId:0,
    carList:[],
    cNum:0,
    cNum1:0,
    isShow:0,
    pid: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.datelist()
    let code = JSON.parse(options.code)
    console.log(code);
    console.log(app.globalData.openId + ":" + app.globalData.access_token)
    // console.log(options.code ? 'http://101.132.97.213/newcode/product/show.htm?wcode=' + options.code : options.url)
    that.setData({
      product: code,
      productId:code.id,
      province: app.globalData.province,
      city: app.globalData.city,
      county: app.globalData.county,
      //carNumber: app.globalData.carNumber,
     
    });
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   *   //加载视频
   */
  onReady: function() {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.datelist()
    
  },
  //跳转购物车
  btn_Cart: function() {
    this.btn_joinCart(),//先加入购物车在跳转
    wx.navigateTo({
      url: "../my/mycart/index"
    })
  },
  //跳转购物车
  btn_Cart1: function () {  
      wx.navigateTo({
        url: "../my/mycart/index"
      })
  },
  //购物车数量
  btn_joinCart: function() {
    var that = this;
    
    for (let i = 0; i < that.data.carList.length; i++) {
      if (that.data.productId == that.data.carList[i].productId) {
        console.log("有了" + that.data.productId)
        that.setData({
          isShow: 1,
          cNum1: that.data.carList[i].count,
          pid: that.data.carList[i].id
        });
      } else{
        that.setData({
          isShow: 0,
          
        });
      }
    }
    that.setData({
      carNumber: that.data.carNumber + 1,
    });
    if (that.data.isShow==1) {
      console.log("有了o" + that.data.productId)      
      that.you();
    } else {
      console.log("没有哦" + that.data.productId)
      that.wu();
    }
    wx.showToast({
      title: `添加成功！`,
      icon: 'success',
      duration: 500
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var that = this;
    
    that.fenxiang();
    var goods_id = 1; //获取产品id
    var goods_title = that.data.product.name; //获取产品标题
    // var goods_img = 'http://101.132.97.213:9090/image/1233212212212/1575952652583827.jpg';//产品图片
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: goods_title,
        path: '/pages/details/index?code=' + that.data.data,
        //imageUrl: goods_img //不设置则默认为当前页面的截图
      }
    }
  },
  //提交分享
  fenxiang: function() {
    wx.request({
      //url: app.globalData.the_host + `/fcode/wechatshare`,
      url: app.globalData.the_host + `/fcode/productshare?tenant=t1`,
      data: {
        "productId": this.data.productId,
        "city": app.globalData.city, // 可选
        "province": app.globalData.province, // 可选
        "address": app.globalData.county, // 可选
        "lat": app.globalData.lat,
        "lng": app.globalData.lng,
        "openId": app.globalData.openId, //必填
      },
      header: {
        'content-type': 'application/json'
      },
      method: "POST",
      success: res => {
        if (res.statusCode == 200) {
          if (res.data.status == 0) {
            console.log(res.data)
          }
        }
      }
    })
  },
  /**
  * 获取购物车数量
  */
  datelist: function () {
    var that = this;
    wx.request({
      url: app.globalData.the_host + `/shopping/shoppingcarts?openid=` + app.globalData.openId,
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var a=0;
        for (let i = 0; i < res.data.length; i++) {
          a += res.data[i].count;
        }
        console.log(a)
        that.setData({
          carList: res.data,
          carNumber:a,
        });
      },
    })
  },
  //购物车 没有
  wu:function(){
    var that = this;
    var count = that.data.cNum + 1
    console.log("无" + count)
    wx.request({
      // url: app.globalData.the_host + '/newcode/api/scan2.htm',//d1950600-f453-4d2b-88c1-4f6752d93212
      url: app.globalData.the_host + `/shopping/shoppingcart`,
      data: {
        "openId": app.globalData.openId,//app.globalData.access_token, // access_token
        "productId": that.data.productId,
        "count": count,
        "tenant": "t1",
        "status": 0
      },
      header: {
        'content-type': 'application/json'
      },
      method: "POST",
      success: res => {
        if (res.statusCode == 200) {
            console.log(res.data)
            that.datelist()
        } else {
          console.log("失败")
        }
      }
    })
  },
  //购物车 有
  you: function () {
    var that = this;
    var count = that.data.cNum1 + 1
    console.log("有" + count)
    wx.request({
      // url: app.globalData.the_host + '/newcode/api/scan2.htm',//d1950600-f453-4d2b-88c1-4f6752d93212
      url: app.globalData.the_host + `/shopping/shoppingcart?id=` + that.data.pid + '&count=' + count,
      data: {},
      header: {
        'content-type': 'application/json'
      },
      method: "PUT",
      success: res => {
        if (res.statusCode == 200) {       
            console.log(res.data)
            that.datelist()      
        } else {
          console.log("失败")
        }
      }
    })
  }
})